﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
using System.Security.Principal;

namespace GesWork.maestros
{
    public partial class insertar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string padre = Request.QueryString["padre"];
            if (padre != null)
            {
                ACC_IDParentActivityTextBox.Text = padre;
                ACC_IDParentActivityTextBox.Enabled = false;
                lnkAddParent.Enabled = false;
            }
            ////btnModificar.Visible = true;
            ////btnEliminar.Visible = true;

            //if (id == null)
            //{
            //    //Pagina para insertar Activiadd
            //    //FormView1.ChangeMode(FormViewMode.Insert);
            //    //btnModificar.Visible = false;
            //    //btnEliminar.Visible = false;
            //}

            //else
            //{
            //    try
            //    {
            //        if (!Page.IsPostBack)
            //        {
            //            //Label toUserC = (Label)FormView1.Row.FindControl("IDToUserLabelContact");
            //            //Label toUserE = (Label)FormView1.Row.FindControl("IDToUserLabelEnterprise");
            //            //Label toUserO = (Label)FormView1.Row.FindControl("IDToUserLabelOportunity");
            //            //Label type = (Label)FormView1.Row.FindControl("OwnerByTypeLabel");


            //            //if (type.Text == "0")
            //            //    toUserO.Visible = true;
            //            //else if (type.Text == "1")
            //            //    toUserE.Visible = true;
            //            //else if (type.Text == "2")
            //            //    toUserC.Visible = true;
            //        }

            //    }
            //    catch (Exception ex)
            //    {
            //        Response.Write(ex.Message);
            //    }


            //}

            //try
            //{
            //    //Obtenemos y asignamos el usuario
            //    Label txtPropietario = (Label)FormView1.Row.FindControl("OwnerLabel");
            //    txtPropietario.Text = User.Identity.Name;
            //    dsActividades.InsertParameters["Owner"].DefaultValue = User.Identity.Name;
            //}
            //catch (Exception ex)
            //{
            //    Response.Write(ex.Message);
            //}

            Page.ClientScript.GetPostBackEventReference(this, String.Empty);

        }

        protected void TipoRelacionInsert(object sender, EventArgs e)
        {
            try
            {
                
                if (cmbOwnerByTypeIns.SelectedValue == "0")
                {
                    CmbIDToUserIns.DataSourceID = "dsOportunityIns";
                }
                else if (cmbOwnerByTypeIns.SelectedValue == "1")
                {
                    CmbIDToUserIns.DataSourceID = "dsEnterpriseIns";
                }
                else if (cmbOwnerByTypeIns.SelectedValue == "2")
                {
                    CmbIDToUserIns.DataSourceID = "dsContactIns";
                }

                //Response.Write("<br>OWNER: " + dsActividades.InsertParameters["owner"].DefaultValue);
                //Response.Write("<br>OwnerByType: " + dsActividades.InsertParameters["OwnerByType"].DefaultValue);
                //Response.Write("<br>IDToUser: " + dsActividades.InsertParameters["IDToUser"].DefaultValue);
                //Response.Write("<br>Subject: " + dsActividades.InsertParameters["Subject"].DefaultValue);
                //Response.Write("<br>MSG_Text: " + dsActividades.InsertParameters["MSG_Text"].DefaultValue);
                //Response.Write("<br>StartDate: " + dsActividades.InsertParameters["StartDate"].DefaultValue);


                CmbIDToUserIns.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }


        }


        protected void btnInsertar_Click(object sender, EventArgs e)
        {

            dsActividades.InsertParameters["Owner"].DefaultValue = User.Identity.Name;
            dsActividades.InsertParameters["OwnerByType"].DefaultValue = cmbOwnerByTypeIns.SelectedValue;
            dsActividades.InsertParameters["IDToUser"].DefaultValue = CmbIDToUserIns.SelectedValue;
            dsActividades.InsertParameters["Subject"].DefaultValue = SubjectTextBox.Text;
            dsActividades.InsertParameters["MSG_Text"].DefaultValue = MSG_TextTextBox.Text;
            dsActividades.InsertParameters["StartDate"].DefaultValue = StartDateTextBox.Text;
            dsActividades.InsertParameters["DueDate"].DefaultValue = DueDateTextBox.Text;
            dsActividades.InsertParameters["ACC_Type"].DefaultValue = cmbACC_TypeTextBox.SelectedValue;
            dsActividades.InsertParameters["ACC_State"].DefaultValue = cmbACC_StateTextBox.SelectedValue;
            dsActividades.InsertParameters["ACC_IDPriority"].DefaultValue = cmbACC_IDPriorityTextBox.SelectedValue; ;
            dsActividades.InsertParameters["ACC_Categories"].DefaultValue = ACC_CategoriesTextBox.Text;
            //dsActividades.InsertParameters["ACC_IDCampaign"].DefaultValue=.Text;
            dsActividades.InsertParameters["ACC_IDParentActivity"].DefaultValue = ACC_IDParentActivityTextBox.Text;
            //dsActividades.InsertParameters["FaxOrPhone"].DefaultValue=.Text;
            dsActividades.InsertParameters["AllDayEvent"].DefaultValue = AllDayEventCheckBox.Checked.ToString();
            //dsActividades.InsertParameters["Layout"].DefaultValue=.Text;
            //dsActividades.InsertParameters["RecurrencePattern"].DefaultValue=.Text;

            dsActividades.Insert();

            Response.Redirect("activas.aspx");
        }

        protected void btnSelectActivity_Click(object sender, EventArgs e)
        {

            //Nos aseguramos que esta vacia
            ACC_IDParentActivityTextBox.Text = "";
           
            //Recoreremos el Repeater regogiendo los que estan checados
            for (int i=0; i < Repeater1.Items.Count; i++)
            {
                if (((RadioButton)Repeater1.Items[i].FindControl("activ")).Checked)
                {
                    ACC_IDParentActivityTextBox.Text += ((Label)Repeater1.Items[i].FindControl("lblID")).Text + " ";
                }
                
            }
                
        }


    }

   

}
